function searchEventToServlet()
{
    console.log('call search servlet');
    var jobj = new Object();
    jobj.sort_by = sort_by;
    jobj.keyword = keyword;
    jobj.user = userID;
    
    httpPost("searchevent",jobj,searchEventResponse);
}
function searchEventResponse(data)
{
    console.log('search servlet response');
    var obj = JSON.parse(data);
    rawEvents =obj.events;
    if(rawEvents)
    {
        _events = new Array(rawEvents.length);
        
        eventResponseCount = 0;
        eventNum = _events.length;
        console.log(eventNum);
        if(eventNum>0)
        {
            for(var i=0;i<eventNum;i++)
            {
                getFBEventData(rawEvents[i].id,_events,i);
                console.log('show sssingle event');
            }  
        }
    }
    else
    {
        eventContent.innerHTML = "沒有活動符合關鍵字";
    }
     
}
function getFBEventData(id,eventObj,i)
    {
        console.log('FB single event request'+id);
        FB.api(
            "/"+id+"?fields=id,name,picture,start_time,location,description",
            function (response) {
                eventResponseCount++;
              if (response && !response.error) {
                /* handle the result */
               //send to database to save
               eventObj[i] = response;
               console.log(response);
               console.log('FB single event response'+id);
              
               console.log('werewshow single event');
              }
              else
               console.log(response);
               if(eventResponseCount==eventNum)
                FBeventLoadingOK();
            }
        );
         console.log('show single event');
         console.log('show sssingle event');  
    }
 
function FBeventLoadingOK()
{
     loadEvents();
     console.log('sdfshow single event');
}
var checkImg = function (src){
   var jqxhr = $.get(src, function() {
       console.log('succeess');
     return true;
   }).fail(function() {
       console.log('fail'); 
    return false;
   });
   return jqxhr;
}
var loadEvents = function ()
{
    console.log('show single event');
    var outputString="";
    for(var i = 0;i<_events.length;i++)
    {
        var pictureURL = _events[i].picture.data.url;
       
       outputString+="<div class='event' id="+i+">"
                        +"<div>";
                            if(checkImg(pictureURL))
                            {
                            outputString+=
                            "<div class='image' width:100>"
                                +"<img src="
                                + pictureURL
                                +" >"
                            +"</div>";
                            }
                            outputString+=
                            "<div style='float:right'>"
                                +"<input type='button' class='likeBtn' onclick='return false'>"
                                +"<p class='interestedNumber'>"+rawEvents[i].interestedNumber+"人覺得有趣</p>"
                            +"</div>"
                            +"<h1 class='title'  >"
                                +_events[i].name
                            +"</h1>"
                           +"<hr/>"
                        +"</div>"
                        
                       +"<div class = 'discription'>"
                             +"<p>時間:"+_events[i].start_time+"</p>"
                             +"<p>地點:"+_events[i].location+"</p>"
                        +"</div>"
                      +"</div>"
                      
    }
   
    eventContent.innerHTML = outputString;
    $('.title').click(
            function(){
                currentEventObj = _events[parseInt($(this).parents('.event').attr('id'))];
                currentEventObj.rawEvent = rawEvents[parseInt($(this).parents('.event').attr('id'))];
                currentEventObj.i = parseInt($(this).parents('.event').attr('id'));
                eventID =currentEventObj.id;
                getCommentfromServlet();
            }
    );
    likeEnable();
    console.log('show single event');
}